
var mapObj;

var popup = "";
var markers = '';
var marker;
var feature;
var arrestGrid;
var pastTitle="";
var drugCompareForm;
var drug1name;
var drug2name;
var arrestquantiy ='';
var counter=0;
var searchvectors='';
var searchdrugCode = "";
var firstpopup = 0;

var firstDrug;

function poi(x, y,record) {
	
	if (popup != "")
	{   
		mapObj.removePopup(popup);
    	popup.destroy();
    	popup = "";
	}
	
	  if(popupkml != '') {
    	 mapObj.removePopup(popupkml);
       
        popupkml='';
       
    }
	var type = record.data.type;
	var detail ="";
	searchdrugCode= record.data.drugcode;
	if(type ==0){
		detail = " <br>"+caseTitle+"  -- "+record.data.noofcases+"<br>"+arrestTitle+"  -- "+
		record.data.noofarrest;
	}else{
		detail = " <br>"+quantityTitle+"--"+record.data.quantity;
	}
	
	var zoom = 7;
	var lonLat = new OpenLayers.LonLat(x, y);
	popup = "";
	var html = "<table class='popupHead'><tr>"+cityLabel+" --" + record.data.admin_en+
			 "</tr><tr>"+periodTitle+" --"+record.data.year_ar+detail+"</tr>"+
		" <tr><img src=images/icons/line.png></tr> <tr> <img src=images/icons/Chart.jpg onclick=createChartforCases('"+record.data.id+""
		+"','"+record.data.type+"','"+2+"','"+record.data.regioncode+"')></tr><tr><a href=javascript:createChartforCases('"
		+record.data.id+"','"+record.data.type+"','"+2+"','"+record.data.regioncode+"')>"+moreDetailsLabel+"</a> </tr></table>";

	popup = new OpenLayers.Popup.FramedCloud("", lonLat, new OpenLayers.Size(200,200), html, null, true, closePopup);
	
	popup.autoSize=true;
	
	if(firstpopup == 0){
		popup = "";
popup = new OpenLayers.Popup.FramedCloud("", lonLat, new OpenLayers.Size(200,200), html, null, true, closePopup);
	
	popup.autoSize=true;
	firstpopup = firstpopup +1;

	}
	
	
	mapObj.addPopup(popup);
		
    mapObj.panTo(new OpenLayers.LonLat(x,y));
	mapObj.setCenter();
}

function closePopup() {
	if (popup != '' && popup != null){
		mapObj.removePopup(popup);
	popup.destroy();
	popup = '';
	}
		if (popupkml != '' && popupkml != null){
		mapObj.removePopup(popupkml);
	popupkml.destroy();
	popupkml = '';
	}
}

function mousedown(evt) {
    if (popup == null) {
        popup = feature.createPopup(true);
        popup.setContentHTML("<div style='background-color:red; width:150;height:100'><a href='http://www.somethingconstructive.net' target='_blank'>click me</a></div>");
        popup.setBackgroundColor("yellow");
        popup.setOpacity(0.7);
        markers.mapObj.addPopup(popup);
    } else {
        popup.toggle();
       }
    OpenLayers.Event.stop(evt);
}

function clearAll(){
if (popup != "")
	{   
		mapObj.removePopup(popup);
    	//popup.destroy();
    	popup = "";
	}
	 
	if (popupkml != "")
	{   
		mapObj.removePopup(popupkml);
		popupkml.destroy();
		popupkml = "";
	}
	 if(markers != ''){
			mapObj.removeLayer(markers);
			markers.clearMarkers();
	        markers = "";
		}
	 if (kmlLayer != "")
		{   
			mapObj.removePopup(kmlLayer);
			kmlLayer.destroy();
			kmlLayer = "";
		}

		if((searchvectors != null)&&(searchvectors != "")){
	mapObj.removeLayer(searchvectors);
searchvectors = "";
}
var win = Ext.getCmp('winChart');
if(win != null)
	win.close();

if( Ext.getCmp('measureWin') != null)
	Ext.getCmp('measureWin').hide();
var ele = document.getElementById("legendpan");
	
	
    		ele.style.display = "none";
	//var ele = document.getElementById("legendpan");
	
	
    		//ele.style.display = "none";

}


var kmlLayer = '';
var  selectControl, selectedFeature;
var popupkml = '';

function onFeatureSelect(event) {
	if (popupkml != '' && popupkml != null){
		mapObj.removePopup(popup);
		popupkml.destroy();
		popupkml = '';
	}
    var feature = event.feature;
    
    
    
    
    var k =0;
	if(feature.attributes.type=="Quantity Drug")
		k=1;

	 var content= "<div id='popupHead' >"+cityLabel+" --" + feature.attributes.admin_en
			+ "<br>"+periodTitle+" --"+feature.attributes.period+" "+feature.attributes.month+"</br>"+
		" "+feature.attributes.type+"  "+feature.attributes.qunatity+"<br><img src=images/icons/line.png></br>  <img src=images/icons/Chart.jpg onclick=createChartforCases('"+feature.attributes.period+"','"+feature.attributes.gid+"','"+feature.attributes.drugId+"','"+k+"')><a href=javascript:createChartforCases('"+feature.attributes.period+"','"+feature.attributes.gid+"','"+feature.attributes.drugId+"','"+k+"')>"+moreDetailsLabel+"</a>  </div>";
  
	 popupkml = new OpenLayers.Popup.FramedCloud("chicken", 
                             feature.geometry.getBounds().getCenterLonLat(),
                              new OpenLayers.Size(null),
                             content,
                             null, true, onPopupClose);
	  popupkml.autoSize = true;
    feature.popup = popupkml;
    mapObj.addPopup(popupkml);
    
}
function onFeatureUnselect(event) {
    feature = event.feature;
    if(feature.popup) {
    	 mapObj.removePopup(feature.popup);
        feature.popup.destroy();
        popupkml='';
        delete feature.popup;
    }
   
 
}

function onPopupClose(evt) {
	//selectControl.unselectAll();
	mapObj.removePopup(popup);
		popupkml.destroy();
		popupkml = '';
}






function onFeatureSelectSearch(event) {
	if (popupkml != '' && popupkml != null){
		mapObj.removePopup(popup);
		popupkml.destroy();
		popupkml = '';
	}
	if (popup != "")
	{   
		mapObj.removePopup(popup);
    	popup.destroy();
    	popup = "";
	}
    var feature = event.feature;
    
    searchdrugCode=  "";
    
    
   var detail ="";
	if(feature.data.report == 0){
		detail = caseTitle+"  -- "+feature.data.actualquantity;
	}else{
		detail = quantityTitle+" --"+feature.data.actualquantity;
	}
	 var content= "<table class='popupHead'><tr>"+cityLabel+" --" + feature.data.name
			+ "</tr><tr>"+periodTitle+" --"+feature.data.years+" "+	" </tr><tr> "+detail+"</tr><tr><img src=images/icons/line.png></tr> <tr> <img src=images/icons/Chart.jpg onclick=createChartforCases('"+feature.data.id+"','"+ feature.data.report+"','"+1+"','"+feature.data.region+"')></tr><tr><a href=javascript:createChartforCases('"+feature.data.id+"','"+ feature.data.report+"','"+1+"','"+feature.data.region+"')>"+moreDetailsLabel+"</a> </tr> </table>";
   
	

	  popupkml = new OpenLayers.Popup.FramedCloud("chicken", 
                             feature.geometry.getBounds().getCenterLonLat(),
                              new OpenLayers.Size(220,220),
                             content,
                             null, true, onPopupClose);
	  popupkml.autoSize = true;

	  	if(firstpopup == 0){
		popupkml = "";
popupkml = new OpenLayers.Popup.FramedCloud("chicken", 
                             feature.geometry.getBounds().getCenterLonLat(),
                              new OpenLayers.Size(220,220),
                             content,
                             null, true, onPopupClose);
	
	popupkml.autoSize=true;
	firstpopup = firstpopup +1;

	}
    feature.popup = popupkml;
    mapObj.addPopup(popupkml);
    
}
function onFeatureUnselectSearch(event) {
    feature = event.feature;
    if(feature.popup) {
    	 mapObj.removePopup(feature.popup);
        feature.popup.destroy();
        popupkml='';
        delete feature.popup;
    }
   

    
    
    
}

function onPopupCloseSearch(evt) {
	
	selectControl.unselectAll();
	popupkml='';
}

function doAddCheckBoxComp(){
var cmp = Ext.getCmp('drug'+0);
				if(Ext.getCmp(firstDrug) != null){ 
				Ext.getCmp(firstDrug).remove(cmp);
			
cmp = Ext.getCmp('year'+0);
				Ext.getCmp(firstDrug).remove(cmp);
				
			var 	CheckboxCombo = new Ext.ux.form.CheckboxCombo( {
		
	
		id:'drug' + 0,
	hiddenName: 'drugId',
	fieldLabel : drugTitle,	
		triggerAction : 'all',
	store: drugStore,
			valueField: 'drugCode',
			allowBlank : false,
	displayField: drugComboDisplayField

	});	
	Ext.getCmp(firstDrug).insert(0,CheckboxCombo);
	Ext.getCmp(firstDrug).insert(3,{
		 

id:"year"+0,		
	xtype: 'checkboxcombo',
	
		mode : 'local',
		fieldLabel : yearsLabel,
		style : 'font-size:11px;',
		emptyText : '',
				allowBlank : false,
		store : new Ext.data.ArrayStore( {
			style : 'font-size:11px;',
			fields : [ 'value', 'label' ],
			data : [ [ '1', '1421' ], [ '2', '1422' ],
					[ '3', '1423' ], [ '4', '1424' ],
					[ '5', '1425' ], [ '6', '1426' ],
					[ '7', '1427' ], [ '8', '1428' ]]
		}),
		valueField : 'value',
				displayField : 'label'



	});
	Ext.getCmp('searchTotal').layout.setActiveItem(10);
	}
}

